﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace BusinessLogic
{
    public class ChiTietDonHangKH
    {
        public Entities.ChiTietDonHangKH[] SelectChiTietDonHangKH(string madhhkh)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            Entities.ChiTietDonHangKH[] ctddhncc = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_ChiTietDonHangKH_Select";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@MaDonDatHangKH", SqlDbType.VarChar, 50).Value = madhhkh;

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                adapter.Fill(ds, "ctddhkh");
                dt = ds.Tables["ctddhkh"];

                var temp = from ddh in dt.AsEnumerable()
                           select new Entities.ChiTietDonHangKH
                           {
                               MaDonDatHangKH = ddh.Field<string>("MaDonDatHangKH"),
                               MaHangHoa = ddh.Field<string>("MaHangHoa"),
                               TenHangHoa = ddh.Field<string>("TenHangHoa"),
                               SoLuong = ddh.Field<string>("SoLuong"),
                               Gia = ddh.Field<string>("DonGia"),
                               PTKhuyenMai = ddh.Field<string>("PhanTramKhuyenMai"),
                               TongTien = ddh.Field<string>("ThanhTien"),
                               Thue = ddh.Field<string>("Thue"),
                               HanSuDung = ddh.Field<DateTime>("HanSuDung"),
                               GhiChu = ddh.Field<string>("GhiChu"),
                               Deleted = ddh.Field<bool>("Deleted"),
                           };
                ctddhncc = temp.ToArray<Entities.ChiTietDonHangKH>();
            }
            catch
            {
                ctddhncc = new Entities.ChiTietDonHangKH[0];
            }
            finally
            {
                con.Close();
            }
            return ctddhncc;
        }
        public bool InsertChiTietDonHangKH(Entities.ChiTietDonHangKH[] ctddhkh, Entities.DonDatHangKH ddhkh)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            bool i = false;

            try
            {
                SqlTransaction tran = con.BeginTransaction("tran");
                try
                {
                    SqlCommand acmd = new SqlCommand();
                    acmd.CommandText = "sp_DonHangKH_Insert";
                    acmd.CommandType = CommandType.StoredProcedure;
                    acmd.Connection = con;

                    acmd.Parameters.Add("@MaDonDatHangKH", SqlDbType.VarChar, 50).Value = ddhkh.MaDonDatHangKH;
                    acmd.Parameters.Add("@NgayDonHang", SqlDbType.DateTime).Value = ddhkh.NgayDonHang;
                    acmd.Parameters.Add("@MaKhachHang", SqlDbType.VarChar, 50).Value = ddhkh.MaKhachHang;
                    acmd.Parameters.Add("@TenKhachHang", SqlDbType.NVarChar, 200).Value = ddhkh.TenKhachHang;
                    acmd.Parameters.Add("@NoHienThoi", SqlDbType.Float).Value = ddhkh.NoHienThoi;
                    acmd.Parameters.Add("@TrangThaiDonDatHang", SqlDbType.NVarChar, 20).Value = ddhkh.TrangThaiDonDatHang;
                    acmd.Parameters.Add("@NgayNhapDuKien", SqlDbType.DateTime).Value = ddhkh.NgayNhapDuKien;
                    acmd.Parameters.Add("@HinhThucThanhToan", SqlDbType.NVarChar, 20).Value = ddhkh.HinhThucThanhToan;
                    acmd.Parameters.Add("@MaKho", SqlDbType.VarChar, 50).Value = ddhkh.MaKho;
                    acmd.Parameters.Add("@TenKho", SqlDbType.NVarChar, 200).Value = ddhkh.TenKho;
                    acmd.Parameters.Add("@MaNhanVien", SqlDbType.VarChar, 50).Value = ddhkh.MaNhanVien;
                    acmd.Parameters.Add("@TenNhanVien", SqlDbType.NVarChar, 200).Value = ddhkh.TenNhanVien;
                    acmd.Parameters.Add("@MaTienTe", SqlDbType.VarChar, 50).Value = ddhkh.MaTienTe;
                    acmd.Parameters.Add("@GhiChu", SqlDbType.VarChar, 50).Value = ddhkh.GhiChu;
                    acmd.Parameters.Add("@Deleted", SqlDbType.Bit).Value = ddhkh.Deleted;

                    acmd.ExecuteNonQuery();

                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = "sp_ChiTietDonHangKH_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = con;

                    foreach (Entities.ChiTietDonHangKH item in ctddhkh)
                    {
                        cmd.Parameters.Clear();
                        cmd.Parameters.Add("@MaDonDatHangKH", SqlDbType.VarChar, 50).Value = item.MaDonDatHangKH;
                        cmd.Parameters.Add("@MaHangHoa", SqlDbType.VarChar, 50).Value = item.MaHangHoa;
                        cmd.Parameters.Add("@TenHangHoa", SqlDbType.NVarChar, 200).Value = item.TenHangHoa;
                        cmd.Parameters.Add("@SoLuong", SqlDbType.Float).Value = item.SoLuong;
                        cmd.Parameters.Add("@DonGia", SqlDbType.Float).Value = item.Gia;
                        cmd.Parameters.Add("@PhanTramChietKhau", SqlDbType.Float).Value = item.PTKhuyenMai;
                        cmd.Parameters.Add("@ThanhTien", SqlDbType.Float).Value = item.TongTien;
                        cmd.Parameters.Add("@Thue", SqlDbType.Float, 20).Value = item.Thue;
                        cmd.Parameters.Add("@HanSuDung", SqlDbType.DateTime).Value = item.Thue;
                        cmd.Parameters.Add("@GhiChu", SqlDbType.NVarChar, 200).Value = item.GhiChu;
                        cmd.Parameters.Add("@Deleted", SqlDbType.Bit, 20).Value = item.Deleted;

                        cmd.ExecuteNonQuery();
                    }

                    tran.Commit();
                    i = true;
                }
                catch (Exception)
                {

                    tran.Rollback("tran");
                }

                LuuVietHeThong.LuuViet("Insert", ddhkh.TaiKhoan.MaNhanVien, "Thêm đơn đặt hàng: " + ddhkh.MaDonDatHangKH, ddhkh.TaiKhoan.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
            }
            catch
            {

                i = false;
            }
            finally
            {
                con.Close();
            }
            return i;
        }

    }
}
